package fdoj.eventInvitations.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fdoj.eventInvitations.entity.Event;
import fdoj.eventInvitations.entity.Person;
import fdoj.eventInvitations.entity.Response;

public class EventController extends HttpServlet{
	private HttpSession session;
	private Person loggedUser;
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		doPost(req, resp);
	}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
	try {
	
		session = request.getSession();
		loggedUser = (Person) session.getAttribute("loggedUser");
		if(loggedUser == null){
			response.sendRedirect(response.encodeRedirectURL("./index"));
			return;
		}
		List<Event> events = (List<Event>) session.getAttribute("events");	
		for(Event e : events){
			if(e.getId() == Integer.parseInt(request.getParameter("id"))){
				session.setAttribute("eventToView", e);
				break;
			}
		}
		if(session.getAttribute("eventToView")==null){
			events = (List<Event>) session.getAttribute("callToIvents");	
			for(Event e : events){
				if(e.getId() == Integer.parseInt(request.getParameter("id"))){
					session.setAttribute("eventToView", e);
					break;
				}
			}
		}
		if(request.getParameter("c").equals("1")){
			request.getRequestDispatcher("/eventAttending.jsp").forward(request, response);
		}else if(request.getParameter("c").equals("2")){
			request.getRequestDispatcher("/eventNotAttending.jsp").forward(request, response);
		}else if(request.getParameter("c").equals("3")){
			request.getRequestDispatcher("/eventMaybeAttending.jsp").forward(request, response);
		}else{
			request.getRequestDispatcher("/eventUnanswered.jsp").forward(request, response);
		}
		return;
	} catch (Exception e) {
		e.printStackTrace();
	
	}
	response.sendRedirect(response.encodeRedirectURL("./error.jsp"));
	}
}
